package cn.acewill.yxd.yunxundian.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import java.util.ArrayList;

import cn.acewill.yxd.yunxundian.model.JsonBean;

/**
 * Created by AAA on 2017/4/5.
 */

public class JsonDb {

    public static final String LOCAL_DATA="yxd_data";
  //  public static final String DATA_ID="Data_id";
    public static final String DATA_TYPE="Data_type";
    public static final String SERVER_ID="Server_id";
    public static final String JSON_STR="Json_Str";

    private JsonDbOpenHelper dbOpenHelper;
    private SQLiteDatabase dbBase;


    public JsonDb(Context context){
        dbOpenHelper=new JsonDbOpenHelper(context);
        dbBase=dbOpenHelper.getWritableDatabase();
    }
    //新增数据
    public boolean addJson(JsonBean jsonBean){
        ContentValues values=new ContentValues();
     //   values.put(DATA_ID,jsonBean.getData_id());
        values.put(DATA_TYPE,jsonBean.getDate_Type());
        values.put(SERVER_ID,jsonBean.getService_Id());
        values.put(JSON_STR,jsonBean.getJson_Str());
        dbBase.insert(LOCAL_DATA,null,values);
        return true;
    }

    public ArrayList<JsonBean> queryJsonData(int id,String type,String serverId){

        ArrayList<JsonBean> list=new ArrayList<JsonBean>();
        String sql="select * from "+LOCAL_DATA+"where 1=1";
//        if(id!=-1){
//            sql+="and"+DATA_ID+"="+id;
//        }
        if(!TextUtils.isEmpty(type)){
            sql+="and"+DATA_TYPE+"="+type;
        }
        if(!TextUtils.isEmpty(serverId)){
            sql+="and"+SERVER_ID+"="+serverId;
        }
        Cursor cursor=dbBase.rawQuery(sql,null);
        while (cursor.moveToNext()){
       //     int data_id=cursor.getInt(cursor.getColumnIndex(DATA_ID));
            int server_id=cursor.getInt(cursor.getColumnIndex(SERVER_ID));
            String data_type=cursor.getString(cursor.getColumnIndex(DATA_TYPE));
            String json_str=cursor.getString(cursor.getColumnIndex(JSON_STR));
            JsonBean jsonBean=new JsonBean(data_type,server_id,json_str);
            list.add(jsonBean);
        }
        cursor.close();
        return list;
    }

}
